package com.automannn.springboot.practice.applicationInitializer;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.util.Collections;

/**
 * @author chenkh
 * @time 2021/9/30 15:59
 */
public class ApplicationInitializerTest implements ApplicationContextInitializer {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication();
        springApplication.setSources(Collections.singleton("com.automannn.springboot.practice.normalBean"));
        springApplication.run(args);
    }

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        /*xxx: 新增监听器*/
        applicationContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                System.out.println("contextInitializer设置的监听器触发了:"+event.getSource());
            }
        });

        ProtocolResolver protocolResolver = new ProtocolResolver() {
            @Override
            public Resource resolve(String location, ResourceLoader resourceLoader) {
                return null;
            }
        };

        /*xxx: 新增协议解析器，加载资源*/
        applicationContext.addProtocolResolver(protocolResolver);


        BeanFactoryPostProcessor postProcessor = new BeanFactoryPostProcessor() {
            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

            }
        };

        /*xxx: 添加容器后置处理器*/
        applicationContext.addBeanFactoryPostProcessor(postProcessor);

        /*xxx: 设置环境信息*/
//        applicationContext.setEnvironment(new StandardEnvironment());
    }
}
